﻿package mortal.game.view.market.qiugou
{
    import Message.Public.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.market.*;
    import mortal.game.view.market.alert.*;
    import mortal.mvc.core.*;

    public class MktQiugouItem extends MktQiugouBase
    {
        private var _numStepper:GNumericStepper;
        private var _comboMType:GComboBox;

        public function MktQiugouItem()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._numStepper.dispose(param1);
            this._comboMType.dispose(param1);
            this._numStepper = null;
            this._comboMType = null;
            super.disposeImpl(param1);
            return;
        }// end function

        override protected function resetUI() : void
        {
            super.resetUI();
            this._numStepper = UIFactory.gNumericStepper(115, 144, 49, 20, this, 999, 1, "NumericStepper", _tfBai);
            this._numStepper.minimum = 1;
            this._numStepper.value = this._numStepper.minimum;
            this._comboMType = UIFactory.gComboBox(218, this._txtUintPrice.y, 55, 22, null, this);
            this._comboMType.dataProvider = MktModConfig.dpMoneyType.clone();
            this._numStepper.configEventListener(Event.CHANGE, this.changeNum);
            this._comboMType.configEventListener(Event.CHANGE, this.onComboMTypeChange);
            _item.configEventListener(MouseEvent.CLICK, this.clickHandler);
            Dispatcher.addEventListener(EventName.MarketPushSeekItem, this.pushItem);
            this.reset(true);
            return;
        }// end function

        private function pushItem(event:DataEvent) : void
        {
            this._item.itemCode = event.data.code;
            return;
        }// end function

        private function onComboMTypeChange(event:Event) : void
        {
            _txtTotalPrice.unit = this._comboMType.selectedItem.moneyType;
            _txtUintPrice.unit = this._comboMType.selectedItem.moneyType;
            updateTax();
            updateUnit();
            return;
        }// end function

        private function changeNum(event:Event) : void
        {
            this.changeTotolPrice();
            return;
        }// end function

        override protected function changeTotolPrice() : void
        {
            _txtTotalPrice.value = _txtUintPrice.value * this._numStepper.value;
            super.changeTotolPrice();
            return;
        }// end function

        override protected function clickHandler(event:MouseEvent) : void
        {
            if (event.currentTarget == _btnReset)
            {
                this.reset();
            }
            else if (event.currentTarget == _btnQiugou)
            {
                this.qiugou();
            }
            else if (event.currentTarget == _item)
            {
                this.reset(true);
            }
            return;
        }// end function

        override protected function reset(param1:Boolean = false) : void
        {
            super.reset(param1);
            if (param1)
            {
                this._item.itemData = null;
                this._numStepper.maximum = 999;
                Dispatcher.dispatchEvent(new DataEvent(EventName.MarketRemoveSeekItem));
            }
            this._numStepper.value = this._numStepper.minimum;
            this._comboMType.selectedIndex = 0;
            this.onComboMTypeChange(null);
            this._timeBox.selectedIndex = 0;
            this.changeTotolPrice();
            return;
        }// end function

        private function qiugou() : void
        {
            if (this._item.itemData == null)
            {
                return;
            }
            if (this._txtUintPrice.value <= 0)
            {
                MsgManager.showRollTipsMsg("请输入正确的价格");
                return;
            }
            var _loc_1:int = 0;
            var _loc_2:* = this._btnBroadcast.selected ? (MktModConfig.BroadcastCost) : (0);
            _loc_2 = _loc_2 + _txtTax.value;
            if (_txtTotalPrice.unit == EPriceUnit._EPriceUnitGold)
            {
                _loc_1 = _loc_1 + _txtTotalPrice.value;
            }
            else if (_txtTotalPrice.unit == EPriceUnit._EPriceUnitCoin)
            {
                _loc_2 = _loc_2 + _txtTotalPrice.value;
            }
            if (!Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitCoinBind, _loc_2))
            {
                MsgManager.showRollTipsMsg("金币不足");
                return;
            }
            if (!Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGold, _loc_1))
            {
                MsgManager.showRollTipsMsg("钻石不足");
                return;
            }
            var _loc_3:* = "你将以" + HTMLUtil.addColor(this._txtUintPrice.value.toString(), GlobalStyle.colorHuang) + GameDefConfig.instance.getEPrictNameAddColoer(this._txtUintPrice.unit) + "的单价收购" + HTMLUtil.addColor(this._numStepper.value.toString(), GlobalStyle.colorHuang) + "个" + ItemsUtil.getItemName(this._item.itemData) + "，求购时间" + HTMLUtil.addColor(this._timeBox.selectedItem.timeType, GlobalStyle.colorHuang) + HTMLUtil.addColor("小时", GlobalStyle.colorHuang);
            var _loc_4:String = "";
            if (this._txtUintPrice.unit == EPriceUnit._EPriceUnitCoin)
            {
                _loc_4 = TodayNoTipsConst.MarketQiugouByCoin;
            }
            else if (this._txtUintPrice.unit == EPriceUnit._EPriceUnitGold)
            {
                _loc_4 = TodayNoTipsConst.MarketQiugouByGold;
            }
            var _loc_5:Object = {};
            _loc_5.unit = this._txtUintPrice.unit;
            _loc_5.str = "求购";
            Alert.extendObj = _loc_5;
            TodayNotTipUtil.toDayNotTip(_loc_4, this.onSelect, _loc_3, null, Alert.OK | Alert.CANCEL, null, null, null, 4, MktTodayNoTipAlert);
            return;
        }// end function

        private function onSelect() : void
        {
            var _loc_1:Object = {};
            _loc_1.code = this._item.itemData.itemCode;
            _loc_1.amount = this._numStepper.value;
            _loc_1.price = this._txtUintPrice.value;
            _loc_1.unit = this._txtTotalPrice.unit;
            _loc_1.time = this._timeBox.selectedItem.timeType;
            _loc_1.broadcast = this._btnBroadcast.selected;
            Dispatcher.dispatchEvent(new DataEvent(EventName.MarketQiugou, _loc_1));
            return;
        }// end function

    }
}
